﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Data.SqlClient;      // For the database connections and objects.

namespace BearingLibUnitTest
{
    /// <summary>
    /// UnitTest1 的摘要说明
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private String source = "server=127.0.0.1,10000;" + "database=unittest;"
         + "Persist Security Info=True;User ID=sa;Password=1qaz2wsx#";

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        //
        // 编写测试时，还可使用以下附加属性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        [TestInitialize()]
        public void MyTestInitialize() 
        {
            using (SqlConnection con = new SqlConnection(source))
            {
                con.Open();
                try
                {
                    using (SqlCommand command = new SqlCommand(
                    "CREATE TABLE Dogs1 (Weight INT, Name TEXT, Breed TEXT)", con))
                    {
                        command.ExecuteNonQuery();
                    }

                }
                catch
                {
                    Console.WriteLine("Table not created.");
                }
            }
        }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
         [TestCleanup()]
        public void MyTestCleanup() {
            using (SqlConnection con = new SqlConnection(source))
            {
                con.Open();
                try
                {
                    using (SqlCommand command = new SqlCommand(
                    "DROP TABLE Dogs1 ", con))
                    {
                        command.ExecuteNonQuery();
                    }

                }
                catch
                {
                    Console.WriteLine("Table not created.");
                }
            }
         }
        //
        #endregion

        /// <summary>
        /// Encapsulates data for dog objects.
        /// </summary>
        public class Dog
        {
            public int Weight { get; set; }
            public string Name { get; set; }
            public string Breed { get; set; }
            public override string ToString()
            {
                return string.Format("Weight: {0}, Name: {1}, Breed: {2}",
                    Weight, Name, Breed);
            }
        }

   
        [TestMethod]
        public void TestMethod2()
        {
     

            using (SqlConnection con = new SqlConnection(source))
            {
                con.Open();
                try
                {
  
                    using (SqlCommand command = new SqlCommand(
                        "INSERT INTO Dogs1 VALUES(@Weight, @Name, @Breed)", con))
                    {
                        command.Parameters.Add(new SqlParameter("Weight", 12));
                        command.Parameters.Add(new SqlParameter("Name", "zzz"));
                        command.Parameters.Add(new SqlParameter("Breed", "dd"));
                        command.ExecuteNonQuery();
                    }
                }
                catch
                {
                    Console.WriteLine("Table not created.");
                }
            }

        }


        /// <summary>
        /// Read in all rows from the Dogs1 table and store them in a List.
        /// </summary>
        [TestMethod]
        public void DisplayDogs()
        {
            List<Dog> dogs = new List<Dog>();
            using (SqlConnection con = new SqlConnection(source))

            {
                con.Open();

                using (SqlCommand command = new SqlCommand("SELECT * FROM Dogs1", con))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        int weight = reader.GetInt32(0);    // Weight int
                        string name = reader.GetString(1);  // Name string
                        string breed = reader.GetString(2); // Breed string
                        dogs.Add(new Dog() { Weight = weight, Name = name, Breed = breed });
                    }
                }
            }
            foreach (Dog dog in dogs)
            {
                Console.WriteLine(dog);
            }
        }
    }
}
